﻿using System;
using System.IO;

/// <summary>
/// Created By : Nguyen Dinh Tuong Uyen
/// Created Date : 02/21/2012
/// Description : Includes method help for file processing
/// </summary>

namespace Common
{
    public class FileHelper
    {
        public FileHelper()
        {
        }

        /// <summary>
        /// Delete the specified file
        /// </summary>
        /// <param name="sPath">File Path</param>
        public static void Delete(string sPath)
        {
            if (sPath != String.Empty)
            {
                if (File.Exists(sPath))
                    File.Delete(sPath);
            }
        }

        /// <summary>
        /// Determines whether the specified file exists
        /// </summary>
        /// <param name="sPath">File Path</param>
        public static bool Exist(string sPath) { return File.Exists(sPath); }

        /// <summary>
        /// Copy an existing file to a new file, not overwriting 
        /// </summary>
        /// <param name="sFileNameOrginal">The path of file want to copy</param>
        /// <param name="sFileNameNew">The path of destination file</param>
        public static void CopyFile(string sFileNameOrginal, string sFileNameNew)
        {
            FileInfo fi = new FileInfo(sFileNameOrginal);
            fi.CopyTo(sFileNameNew);
        }

        /// <summary>
        /// Copy an existing file to a new file, allow Rename if destination file exists
        /// </summary>
        /// <param name="sFileNameOrginal">The path of file want to copy</param>
        /// <param name="sFileNameNew">The path of destination file</param>
        public static string CopyFile(string sFileNameOrginal, string sFileNameNew,bool RenameIfExist)
        {
            string filePath = "";

            string fileNameNoExt = GetFileName_WithoutExtension(sFileNameNew);
            string ext = GetExtension(sFileNameNew);
            string folderPath = sFileNameNew.Replace(fileNameNoExt + "." + ext, "");

            
            if (RenameIfExist)
            {
                while (FileHelper.Exist(folderPath + fileNameNoExt + "." + ext)) //check file exist
                {
                    fileNameNoExt += CommonHelper.RanNum(2);
                }
            }

            filePath = folderPath + fileNameNoExt + "." + ext;
            
            FileInfo fi = new FileInfo(sFileNameOrginal);
            fi.CopyTo(filePath);

            return filePath;

        }

        /// <summary>
        /// Get file extension without "."
        /// </summary>
        /// <param name="FileNameOrFilePath"></param>
        /// <returns></returns>
        public static string GetExtension(string FileNameOrFilePath)
        {
            return Path.GetExtension(FileNameOrFilePath).ToLower().Replace(".", "");
        }

        /// <summary>
        /// Get file name with extension
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string GetFileName_WithExtension(string filePath)
        {
            return Path.GetFileName(filePath);
        }

        /// <summary>
        /// Get file name without extension
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string GetFileName_WithoutExtension(string filePath)
        {
            //get ext without "."
            string ext = GetExtension(filePath);

            //get file name and replace ext
            return Path.GetFileName(filePath).Replace("." + ext, "");
        }

        
    }
}